//
//  FirstTwoTableViewCell.m
//  DaJiHui
//
//  Created by Hao on 2017/9/15.
//  Copyright © 2017年 Hao. All rights reserved.
//
#import "UIImageView+WebCache.h"

#import "FirstTwoTableViewCell.h"

@interface FirstTwoTableViewCell()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *descLabel;
@end

@implementation FirstTwoTableViewCell

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@(15));
        make.width.equalTo(@(90));
        make.height.equalTo(@(80));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imgView.mas_right).offset(15);
        make.top.equalTo(self.mas_top).offset(15);
        [self.titleLabel sizeToFit];
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imgView.mas_right).offset(15);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
        [self.priceLabel sizeToFit];
    }];
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imgView.mas_right).offset(15);
        make.top.equalTo(self.priceLabel.mas_bottom).offset(15);
        [self.descLabel sizeToFit];
    }];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initContentView];
    }
    return self;
}

- (void)initContentView
{
    [self addSubview:self.imgView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.priceLabel];
    [self addSubview:self.descLabel];
}

- (UIImageView *)imgView
{
    if (!_imgView) {
        _imgView = [[UIImageView alloc] init];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imgView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:17];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = [UIColor redColor];
        _priceLabel.font = [UIFont systemFontOfSize:17];
        _priceLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _priceLabel;
}

- (UILabel *)descLabel
{
    if (!_descLabel) {
        _descLabel = [[UILabel alloc] init];
        _descLabel.textColor = [UIColor grayColor];
        _descLabel.font = [UIFont systemFontOfSize:13];
        _descLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _descLabel;
}

- (void)setMyModel:(FirstTwoResModel *)myModel{
    if (_myModel != myModel) {
        _myModel = myModel;
    }
    NSString *url = [self getPicUrl:myModel.pic1];
    if (url.length) {
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.chaojihui123.com%@",[url substringFromIndex:8]]]];
    }
    self.titleLabel.text = myModel.title;
    self.priceLabel.text = [NSString stringWithFormat:@"￥ %@", myModel.price ];
    self.descLabel.text = [NSString stringWithFormat:@"门市价￥:%@", myModel.price ];
}

- (NSString *)getPicUrl:(id)pic
{
    if (pic == NULL || pic == nil) {
        return @"";
    }else if(pic == [NSNull class]){
        return @"";
    }else if (pic == [NSNull null]){
        return @"";
    }else if ([pic isEqualToString:@"null"]){
        return @"";
    }else{
        return pic;
    }
}


@end
